package com.shadt.cloud.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shadt.cloud.dao.ChannelDao;
import com.shadt.cloud.service.ChannelService;
import com.shadt.cloud.vo.ChannelVo;

/**
 * 栏目业务逻辑层实现类
 * 
 * @author SongR
 *
 */
@Service
public class ChannelServiceImpl implements ChannelService {

	@Autowired
	ChannelDao dao;

	/**
	 * 实现获取所有栏目
	 */
	@SuppressWarnings({ "rawtypes" })
	public List<ChannelVo> getAll() {
		List l = dao.getBySql("select * from community_channel where isenable = 1  order by sortno");
		Iterator it = l.iterator();
		List<ChannelVo> vos = new ArrayList<ChannelVo>();
		while (it.hasNext()) {
			Map m = (Map) it.next();
			ChannelVo vo = new ChannelVo();
			vo.setId(m.get("seq").toString());
			vo.setName(m.get("title").toString());
			vo.setKey(m.get("channelkey").toString());
			vos.add(vo);
		}
		return vos;
	}

	@SuppressWarnings("rawtypes")
	public List<ChannelVo> getParent(String start, String size) {
		String hql = "select * from community_channel where isenable = 1 and parentseq is null order by sortno limit "
				+ start + "," + size;
		if ("".equals(start) || "".equals(size) || size.equals("0")) {
			hql = "select * from community_channel where isenable = 1 and parentseq is null order by sortno";
		}
		List l = dao.getBySql(hql);
		Iterator it = l.iterator();
		List<ChannelVo> vos = new ArrayList<ChannelVo>();
		while (it.hasNext()) {
			Map m = (Map) it.next();
			ChannelVo vo = new ChannelVo();
			vo.setId(m.get("seq").toString());
			vo.setName(m.get("title").toString());
			vo.setKey(m.get("channelkey").toString());
			vos.add(vo);
		}
		return vos;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<ChannelVo> getChild(String pid, String start, String size) {
		//包含用户平台id 和 父权限id
		String[] idarrys = pid.split(",");
		String id=idarrys[1];
		pid = idarrys[0];
		String hql = "select * from community_channel where isenable = 1 and parentseq =" + pid
				+ " order by sortno limit " + start + "," + size;
		if ("".equals(start) || "".equals(size) || size.equals("0")) {
			hql = "select * from community_channel where isenable = 1 and parentseq="+pid+" order by sortno";
			/*hql="SELECT cc.*,COUNT(*) AS 'sum',STATUS FROM community_channel cc " +
					"INNER JOIN cms_cloud_branch_channel cb ON cc.channelKey=cb.channel_key" +
					"INNER JOIN cms_cloud_branch cl ON cb.branch_id=cl.branch_id " +
					"WHERE cb.branch_id="+pid+" AND cc.channelKey="+""+"";*/
		}
		List l = dao.getBySql(hql);
		//获取当前用户申请过的权限
		String tsHql="SELECT channel_key as channelKey FROM cms_cloud_branch_channel a WHERE " +
				"EXISTS(SELECT 1 FROM community_channel WHERE isenable = 1 AND parentseq="+pid+" ORDER BY sortno) " +
				"AND branch_id="+id+" and status=0";
		List t_key = dao.getBySql(tsHql);
		//权限列表 （所有子权限）
		List<Map<String, Object>> lmap = (List<Map<String, Object>>) l;
		//当前用户申请过的权限
		List<Map<String, Object>> tmap = (List<Map<String, Object>>) t_key;
		//比较两个listmap中的channelKey
		for(int i=0;i<tmap.size();i++){
			for(int j=0;j<lmap.size();j++){
				if(tmap.get(i).containsValue(lmap.get(j).get("channelKey").toString())){
					lmap.remove(j);
				}
			}
		}
		Iterator it = l.iterator();
		List<ChannelVo> vos = new ArrayList<ChannelVo>();
		while (it.hasNext()) {
			Map m = (Map) it.next();
			ChannelVo vo = new ChannelVo();
			vo.setId(m.get("seq").toString());
			vo.setName(m.get("title").toString());
			vo.setKey(m.get("channelkey").toString());
			vos.add(vo);
		}
		return vos;
	}

	/**
	 * 根据key获取栏目
	 */
	@SuppressWarnings("rawtypes")
	public ChannelVo getByKey(String key) {
		String hql = "select * from community_channel where isenable = 1 and channelkey ="+key  ;
		List l = dao.getBySql(hql);
		Iterator it = l.iterator();
		List<ChannelVo> vos = new ArrayList<ChannelVo>();
		while (it.hasNext()) {
			Map m = (Map) it.next();
			ChannelVo vo = new ChannelVo();
			vo.setId(m.get("seq").toString());
			vo.setName(m.get("title").toString());
			vo.setKey(m.get("channelkey").toString());
			vos.add(vo);
		}
		if (vos.size() > 0)
			return vos.get(0);
		else
			return null;

	}
}
